package org.dbwebx.flex.control.renderer
{
	import mx.containers.Box;
	import mx.controls.listClasses.BaseListData;
	import mx.controls.listClasses.IDropInListItemRenderer;
	
	import org.fxcontainer.controls.ext.FxGrid;
	import org.fxcontainer.controls.ext.FxGridColumn;
	
	public class ChevronActionDataGridRenderer extends Box implements IDropInListItemRenderer
	{
		
		private var _listData:BaseListData;// DataGridListData;
        private var _dataChanged:Boolean = true;
        
		public function ChevronActionDataGridRenderer()
		{
			super();
		}
		
		public function get listData():BaseListData
		{
			return _listData;
		}
		
		public function set listData(value:BaseListData):void
		{
			_listData = value;
		}
		
		override public function set data(value:Object):void
        {
            super.data = value;
            _dataChanged = true;
            invalidateProperties();
        }

        override protected function createChildren():void
        {
            super.createChildren();
            chevron = new Chevron(chevronClickHandler);
            chevron.vanishing = true;
            addChild(chevron);
            //chevron.vanishing = true;
        }

        protected function chevronClickHandler():void
        {
            var dg:FxGrid = FxGrid(listData.owner);
            var dgColumn:FxGridColumn = dg.columns[listData.columnIndex];
            var parentViewComponent:UIComponent = dg.parentDocument as UIComponent;

            var menuGenerator:IMenuGenerator = dgColumn.menuGenerator;
            if(menuGenerator == null)
            {
                throw new IllegalOperationError("Absent menuGenerator in chevron");
            }
            menuGenerator.setupMenuData(data, dg.dataProvider, parentViewComponent);
            menuGenerator.prepareMenu();
            menuGenerator.showMenu(chevron);
        }

        override protected function commitProperties():void
        {
            super.commitProperties();

            if(_dataChanged)
            {
                _dataChanged = false;

               if(!this.contains(chevron))
                {
                    addChild(chevron);
                }
            }
        }
	}
}